#
# This module will, when pulled in, do all required work to
# make a Linux Counter script run fine.
#
# This includes mucking about with @LIB, detecting init files,
# reading init files, chdir()ing to the right directory, and so on.
#
# The required input is COUNTER_ROOT. If it is not found,
# /home/counter is assumed.
#

package LinuxCounter;
use strict;
use vars qw{$root %vars};

# I wish this made sense; however, perl must find LinuxCounter.pm...
#BEGIN {
#    $LinuxCounter::root = $ENV{COUNTER_ROOT};
#    if (!$root) {
#	$root = "/home/counter";
#    }
#    warn "Counter root is $root\n";
#    push(@INC, "$root/lib");
#}

$root = $ENV{COUNTER_ROOT};
if (!$root) {
    $root = "/home/counter";
}
# Read init file. Not thought through yet!
if (open(INI, "$root/.counter.ini")) {
    while (<INI>) {
	chop;
	if (/^(\S+)=/) {
	    my $varname = $1;
	    if ($varname eq "homedir") {
		if ($' ne $root) {
		    die "INI file and given root are inconsistent\n";
		}
	    }
	    $vars{$varname} = $';
	}
    }
} else {
    #warn "No init file\n";
}
 
# If we're a CGI script, do chdir()
# note that this is not what import() usually does!
sub cgi {
    warn "Proc $$ Initializing for CGI\n";
    chdir($root) || die "Unable to chdir to $root\n";
}

1;
